require 'extract/rules/json_rule'
require 'singleton'
require 'enum'


module Extract
  
  class FacebookRules < Enum
    
    public
    
    #define de Brightkite extractions rules
    
    class Fullname < JSONRule
      protected
      
      def get_json_val(doc)
        doc["name"] 
      end
      
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_name =  val
      end
      
    end
    
    class Firstname < JSONRule
      protected
      
      def get_json_val(doc)
        doc["first_name"] 
      end
      
      
      def handle_val(val, dest_receiver)
        dest_receiver.first_name = val
      end
      
    end
    
    class Lastname < JSONRule
      protected
      
      def get_json_val(doc)
        doc["last_name"] 
      end
      
      
      def handle_val(val, dest_receiver)
        dest_receiver.last_name = val
      end
      
    end
    
    class Lang < JSONRule
      protected
      
      def get_json_val(doc)
        val = nil
        arr = []
        arr = doc["locale"].split '_' unless doc["locale"].nil?
        val = arr[0] unless arr[0].nil?
        val
      end 
      
      def handle_val(val, dest_receiver)
        dest_receiver.locale_language = val
      end
      
    end
    
    class LocalCode < JSONRule
      protected
      
      def get_json_val(doc)
        val = nil
        arr = []
        arr = doc["locale"].split '_' unless doc["locale"].nil?
        val = arr[1] unless arr[1].nil?
        val
      end 
      
      def handle_val(val, dest_receiver)
        dest_receiver.locale_country = val
      end
      
    end
    
    class Gender < JSONRule
      protected
      
      def get_json_val(doc)
        val = nil
        val = doc['gender'].eql?('female') ? 'female' : 'male' unless doc['gender'].nil?
        val
      end 
      
      def handle_val(val, dest_receiver)
        dest_receiver.gender = val
      end
      
    end
    
    protected
    
    def initialize
      super()
      @collections[:Fullname] = Fullname.instance
      @collections[:Firstname] = Firstname.instance
      @collections[:Lastname] = Lastname.instance
      @collections[:Lang] = Lang.instance
      @collections[:LocalCode] = LocalCode.instance
      @collections[:Gender] = Gender.instance
    end
    
  end
  
end
